

*set filepath. NOTE: you will need to update the filepath to run this file
cd "/Users/jeremyhorowitz/Documents/Book/Replication files/Chapter 3"



**************************************************************
********** Table 3.1 *****************************************
**************************************************************

use "2007 parl results.dta", clear

gsort connum -votes 
drop if vote_share<5
drop if vote_share==.
sort connum

gen pnu_coal=0
replace pnu_coal=1 if party=="pnu" | party=="kanu" | party=="safina" | ///
party=="dp" |  party=="ford-k" | party=="ford-p" | party=="ford-a" | ///
party=="agano" | party=="narc kenya" | party=="forepa" | ///
party=="shirikisho" | party=="new ford-k" | party=="cdp" | party=="krrp" | ///
party=="mgpk" | party=="nap" | party=="nrpp" | party=="new aspiration party" | /// 
party=="sisi kwa sisi" | party=="saba saba asili" | party=="wcpk" | party=="knda"

gen odm_coal=0
replace odm_coal=1 if party=="odm" | party=="narc"  | party=="united democratic movement"

gen coalition_2007=""
replace coalition_2007="pnu coalition" if pnu_coal==1
replace coalition_2007="odm coalition" if odm_coal==1
replace coalition_2007="odm-k" if party=="odm-k"
replace coalition_2007="other" if coalition_2007==""

tab coalition_2007 if eg=="kikuyu"
tab coalition_2007 if eg=="luo"
tab coalition_2007 if eg=="luhya"
tab coalition_2007 if eg=="kalenjin"
tab coalition_2007 if eg=="kamba"
tab coalition_2007 if eg=="mijikenda"
tab coalition_2007 if eg=="kisii"
tab coalition_2007 if eg=="meru"
tab coalition_2007 if eg=="somali"



**************************************************************
********** Table 3.4 *****************************************
**************************************************************

***2007

clear
use "Sept2007survey.dta"

*variables
recode q26n1 (6=1) (9=1) (5=1) (else=0), gen(coethnic) 
recode q55 (1/12=1) (13=0) (15=0) (14=.) (16=1), gen(feelclose) 
recode q58 (98=1) (95=.) (99=.) (else=0), gen(dk)
recode q86m3 (1=3) (2=1) (3=2) (else=.), gen(kibaki_fav)
recode q86m2 (1=3) (2=1) (3=2) (else=.), gen(odinga_fav)
gen disparity=kibaki_fav - odinga_fav
gen absdisp=abs(disparity)
recode absdisp (0=0) (1/2=1) (else=.), gen(disp_high)

ttest feelclose, by(coeth)
ttest dk, by(coeth)
ttest disp_high, by(coeth)


***2013
use "panel2012.dta", clear

recode q118_r1 (1/2=1) (3/4=0) (.=0) (777=0) (999=.), gen(feel_close_high)
recode absdisp_r1 (0/1=0) (2/3=1) (else=.), gen(disp_high)

ttest feelclose_r1, by(coeth)
ttest feel_close_high, by(coeth)
ttest dk_r1, by(coeth)
ttest disp_high, by(coeth)


***2017
use "panel2017r1.dta", clear

recode D31 (5/6=1) (29=.) (995=.) (else=0), gen(coethnic) 
recode PROP_GQ_KJ98 (19/20=0) (21=.) (else=1), gen(feelclose)
recode PROP_GQ_KJ_CH114 (996=1) (997/999=.) (else=0), gen(dk) 
recode PROP_KJ115_1 (1=4) (2=3) (3=2) (4=1) (else=.), gen(kenyatta_rep)
recode PROP_KJ115_2 (1=4) (2=3) (3=2) (4=1) (else=.), gen(odinga_rep)
gen disparity=kenyatta_rep - odinga_rep
gen absdisp=abs(disparity)
recode absdisp (0/1=0) (2/3=1) (else=.), gen(disp_high)
recode PROP_KJ100 (1=1) (2/3=0) (.=0) (4=.), gen(feel_close_high)

ttest feelclose, by(coeth)
ttest feel_close_high, by(coeth)
ttest dk, by(coeth)
ttest disp_high, by(coeth)



**************************************************************
********** Table 3.5  ****************************************
**************************************************************

use "panel2012.dta", clear

svyset [pw=attweight2] 

*candidate to candidate
gen uhurutoraila=.
replace uhurutoraila=1 if q121b_r1==1 & q121b_r2==2 & q121b_r1!=. & q121b_r2!=.
replace uhurutoraila=0 if uhurutoraila!=1 & q121b_r1!=. & q121b_r2!=.
svy: mean uhurutoraila if match2==3
svy: mean uhurutoraila if match2==3, over(coeth)
lincom [uhurutoraila]1 - [uhurutoraila]0 

gen railatouhuru=.
replace railatouhuru=1 if q121b_r1==2 & q121b_r2==1 & q121b_r1!=. & q121b_r2!=.
replace railatouhuru=0 if railatouhuru!=1 & q121b_r1!=. & q121b_r2!=.
svy: mean railatouhuru if match2==3
svy: mean railatouhuru if match2==3, over(coeth)
lincom [railatouhuru]1 - [railatouhuru]0 

gen cantocan=.
replace cantocan=1 if railatouhuru==1 | uhurutoraila==1
replace cantocan=0 if railatouhuru==0 & uhurutoraila==0
svy: mean cantocan if match2==3
svy: mean cantocan if match2==3, over(coeth)
lincom [cantocan]1 - [cantocan]0 

*don't know to candidate
gen dktouhuru=.
replace dktouhuru =1 if q121b_r1>2 & q121b_r2==2 & q121b_r1!=. & q121b_r2!=.
replace dktouhuru =0 if dktouhuru!=1 & q121b_r1!=. & q121b_r2!=.
svy: mean dktouhuru if match2==3
svy: mean dktouhuru if match2==3, over(coeth)
lincom [dktouhuru]1 - [dktouhuru]0 

gen dktoraila=.
replace dktoraila=1 if q121b_r1>2 & q121b_r2==1 & q121b_r1!=. & q121b_r2!=.
replace dktoraila=0 if dktoraila!=1 & q121b_r1!=. & q121b_r2!=.
svy: mean dktoraila if match2==3
svy: mean dktoraila if match2==3, over(coeth)
lincom [dktoraila]1 - [dktoraila]0 

gen dktocan=.
replace dktocan=1 if dktoraila==1 | dktouhuru==1
replace dktocan=0 if dktoraila==0 & dktouhuru==0
svy: mean dktocan if match2==3
svy: mean dktocan if match2==3, over(coeth)
lincom [dktocan]1 - [dktocan]0 

*candidate to don't know
gen uhurutodk=.
replace uhurutodk=1 if q121b_r1==1 & q121b_r2>2 & q121b_r1!=. & q121b_r2!=.
replace uhurutodk=0 if uhurutodk!=1 & q121b_r1!=. & q121b_r2!=.
svy: mean uhurutodk if match2==3
svy: mean uhurutodk if match2==3, over(coeth)
lincom [uhurutodk]1 - [uhurutodk]0 

gen railatodk=.
replace railatodk=1 if q121b_r1==2 & q121b_r2>2 & q121b_r1!=. & q121b_r2!=.
replace railatodk=0 if railatodk!=1 & q121b_r1!=. & q121b_r2!=.
svy: mean railatodk if match2==3
svy: mean railatodk if match2==3, over(coeth)
lincom [railatodk]1 - [railatodk]0 

gen cantodk=.
replace cantodk=1 if railatodk ==1 | uhurutodk ==1
replace cantodk =0 if railatodk ==0 & uhurutodk ==0
svy: mean cantodk if match2==3
svy: mean cantodk if match2==3, over(coeth)
lincom [cantodk]1 - [cantodk]0 

*total
svy: mean change if match2==3
svy: mean change if match2==3, over(coeth)
lincom [change]1 - [change]0 



**************************************************************
********** Table 3.6  ****************************************
**************************************************************

use "panel2017.dta", clear

svyset [pw=attweight2] 

*candidate to candidate
svy: mean uhurutoraila
svy: mean uhurutoraila, over(coeth)
lincom [uhurutoraila]1 - [uhurutoraila]0 

svy: mean railatouhuru
svy: mean railatouhuru, over(coeth)
lincom [railatouhuru]1 - [railatouhuru]0 

svy: mean cantocan 
svy: mean cantocan , over(coeth)
lincom [cantocan]1 - [cantocan]0 

*don't know to candidate
svy: mean dktouhuru 
svy: mean dktouhuru, over(coeth)
lincom [dktouhuru]1 - [dktouhuru]0 

svy: mean dktoraila
svy: mean dktoraila, over(coeth)
lincom [dktoraila]1 - [dktoraila]0 

svy: mean dktocan 
svy: mean dktocan, over(coeth)
lincom [dktocan]1 - [dktocan]0 


*candidate to don't know
svy: mean uhurutodk
svy: mean uhurutodk, over(coeth)
lincom [uhurutodk]1 - [uhurutodk]0 

svy: mean railatodk 
svy: mean railatodk, over(coeth)
lincom [railatodk]1 - [railatodk]0 

svy: mean cantodk
svy: mean cantodk, over(coeth)
lincom [cantodk]1 - [cantodk]0 

*total
svy: mean change
svy: mean change, over(coeth)
lincom [change]1 - [change]0 


*****************************************************************************
********** Figure 3.1 *******************************************************
*****************************************************************************

***2013 Election
use "panel2012.dta", clear

gen num =_n in 1/9
gen s1=. in 1/9
gen sample=. in 1/9

set more off
local a = 1
while `a' <=9 {
sum change if match2==3 & groupnum_r1==`a' [iweight=attweight2]
replace s1=r(mean) if _n==`a'
replace sample=r(N) if _n==`a'
local a = `a' + 1
}

twoway (bar s1 num if num<3, color(gs10*.8) barw(.8)) (bar s1 num if num>2, color(gs4*.8) barw(.8)), ///
xlabel(1 `" "Kikuyu" " " "[136]" "' 2 `" "Luo" " " "[124]" "' 3 `" "Kalenjin" " " "[100]" "' 4 `" "Kamba" " " "[82]" "' ///
5 `" "Luhya" " " "[152]" "' 6 `" "Kisii" " " "[53]" "' 7 `" "Meru/" "Embu" "[43]" "' 8 `" "Mijikenda" " " "[47]" "' 9 `" "Other" " " "[70]" "') ///
ylabel(0 "0" .05 "5" .1 "10" .15 "15" .2 "20" .25 "25" .3 "30" .35 "35" .4 "40", nogrid) ytitle("Percent of Group") ///
xtitle("") scheme(s1mono) plotregion(lc(white)) title("(a) 2013 Election") ///
legend(off)


***2017 Election
use "panel2017.dta", clear

recode D31 (5=1) (6=2) (4=3) (1=4) (2=5) (9=6) (12=7) (8=7) (17=8) (995=.) (.=.) (else=9), gen(groupnum_r1)

gen num =_n in 1/9
gen s1=. in 1/9
gen sample=. in 1/9

set more off
local a = 1
while `a' <=9 {
sum change if age_r2!=. & groupnum_r1==`a' [iweight=attweight2]
replace s1=r(mean) if _n==`a'
replace sample=r(N) if _n==`a'
local a = `a' + 1
}

twoway (bar s1 num if num<3, color(gs10*.8) barw(.8)) (bar s1 num if num>2, color(gs4*.8) barw(.8)), ///
xlabel(1 `" "Kikuyu" " " "[159]" "' 2 `" "Luo" " " "[109]" "' 3 `" "Kalenjin" " " "[70]" "' 4 `" "Kamba" " " "[72]" "' ///
5 `" "Luhya" " " "[117]" "' 6 `" "Kisii" " " "[48]" "' 7 `" "Meru/" "Embu" "[46]" "' 8 `" "Mijikenda" " " "[27]" "' 9 `" "Other" " " "[79]" "') ///
ylabel(0 "0" .05 "5" .1 "10" .15 "15" .2 "20" .25 "25" .3 "30" .35 "35" .4 "40", nogrid) ytitle("Percent of Group") ///
xtitle("") scheme(s1mono) plotregion(lc(white)) title("(b) 2017 Election") ///
legend(off)



*****************************************************************************
********** Figure 3.2 *******************************************************
*****************************************************************************

***2013
use "panel2012.dta", clear

*estimate model of preference change
set more off 

logit change i.coeth ///
pardiff spousediff c.networknco c.ncoshare ///
c.polint ed_r1 ///
c.radionews_r1 newspaper_r1 tvnews_r1 vernacular ///
wealthindex_r1 age_r1 female_r1 ///
gap if match2==3 [pweight=attweight2]

margins coeth, saving(j1, replace)
margins, dydx(coeth) atmeans post saving(j2, replace)


***2017
use "panel2017.dta", clear

*estimate model of preference change
set more off
logit change i.coeth ///
pardiff spousediff polint_r1 ncoshare ed newspaper_r1 tv_r1 radio_r1 house_income_r1 age_r2 female_r1 [pweight=attweight2]

margins coeth, saving(j3, replace)
margins, dydx(coeth) atmeans post saving(j4, replace)


***Plot
use j1, clear
append using j2
append using j3
append using j4

egen can=seq(), from(0) to(2)
gen xplace=_n
gen order=_n

replace xplace=.90 if order==1
replace xplace=1 if order==2
replace xplace=1.1 if order==3
replace xplace=1.90 if order==4
replace xplace=2 if order==5
replace xplace=2.1 if order==6

format _margin %8.2f 

replace _margin = abs(_margin)
replace _ci_lb = abs(_ci_lb)
replace _ci_ub = abs(_ci_ub)

scatter _margin xplace if can==0 & xplace<1.2, mcolor(black) msymbol(O) msize(medium) mlabel(_margin) mlabposition(9) ||  ///
rspike _ci_lb _ci_ub xplace if can==0 & xplace<1.2, color(black) || ///
scatter _margin xplace if can==1 & xplace<1.2, mcolor(black) msymbol(T) msize(medium) mlabel(_margin) mlabposition(9)  mlabgap(*2) ||  ///
rspike _ci_lb _ci_ub xplace if can==1 & xplace<1.2, color(black)  || ///
scatter _margin xplace if can==2 & xplace<1.2, mcolor(black) msymbol(Dh) msize(medium) mlabel(_margin) mlabposition(3)  mlabgap(*2) ||  ///
rspike _ci_lb _ci_ub xplace if can==2 & xplace<1.2, color(black) ///
xscale(range(0.8 1.2) off) yscale(range(0 .3)) scheme(s1mono) ///
xtitle(" ") ylabel(0 "0" .1 ".1" .2 ".2" .3 ".3" .4 ".4", angle(0)) ///
legend(order(1 3 5) pos(6) col(1) label(1 "No coethnic in race") label(3 "Coethnic in race") label(5 "Difference")) ///
plotregion(lcolor(white)) ytitle("Pr(Change Vote)") ylin(0, lw(thin)) ///
xsize(5) aspectratio(1) scale(1.3) yscale(noextend) title("2013 Election") name(g1, replace)

scatter _margin xplace if can==0 & xplace>1.2, mcolor(black) msymbol(O) msize(medium) mlabel(_margin) mlabposition(9) ||  ///
rspike _ci_lb _ci_ub xplace if can==0 & xplace>1.2, color(black) || ///
scatter _margin xplace if can==1 & xplace>1.2, mcolor(black) msymbol(T) msize(medium) mlabel(_margin) mlabposition(9)  mlabgap(*2) ||  ///
rspike _ci_lb _ci_ub xplace if can==1 & xplace>1.2, color(black)  || ///
scatter _margin xplace if can==2 & xplace>1.2, mcolor(black) msymbol(Dh) msize(medium) mlabel(_margin) mlabposition(3)  mlabgap(*2) ||  ///
rspike _ci_lb _ci_ub xplace if can==2 & xplace>1.2, color(black) ///
xscale(range(1.8 2.2) off) yscale(range(0 .3)) scheme(s1mono) ///
xtitle(" ") ylabel(0 "0" .1 ".1" .2 ".2" .3 ".3" .4 ".4", angle(0)) ///
legend(order(1 3 5) pos(6) col(1) label(1 "No coethnic in race") label(3 "Coethnic in race") label(5 "Difference")) ///
plotregion(lcolor(white)) ytitle("Pr(Change Vote)") ylin(0, lw(thin)) ///
xsize(5) aspectratio(1) scale(1.3) yscale(noextend) title("2017 Election") name(g2, replace)

graph combine g1 g2, scheme(s1mono) xsize(8)



*****************************************************************************
********** Tables 3.7 & 3.8 *************************************************
*****************************************************************************

***2013
use "panel2012.dta", clear

recode q118_r1 (1/2=1) (3/4=0) (.=0) (777=0) (999=.), gen(feel_close_high)

logit change feelclose_r1 if match2==3  [pweight=attweight2]
margins, at(feelclose_r1=(0 1))

logit change feel_close_high if match2==3  [pweight=attweight2]
margins, at(feel_close_high=(0 1))

logit change dk_r1 if match2==3  [pweight=attweight2]
margins, at(dk_r1=(0 1))

logit change absdisp_r1 if match2==3  [pweight=attweight2]
margins, at(absdisp_r1=(0 3))


***2017
use "panel2017.dta", clear

logit change feelclose_r1 [pweight=attweight2]
margins, at(feelclose_r1=(0 1))

logit change feel_close_high [pweight=attweight2]
margins, at(feel_close_high=(0 1))

logit change dk_r1 [pweight=attweight2]
margins, at(dk_r1=(0 1))

logit change absdisp_r1 [pweight=attweight2]
margins, at(absdisp_r1=(0 3))



*****************************************************************************
********** Table 3.9 & Figure 3.3 *******************************************
*****************************************************************************

***2013 Election
use "panel2012.dta", clear

*Table 3.9, model 1
set more off
logit newuhuru uhururep_ch railarep_ch  ///
uhururep_r1 railarep_r1 ///
likeu_ch liker_ch ///
pardiff spousediff networknco ncoshare vernacular ///
polint ed_r1 ///
radionews_r1 newspaper_r1 tvnews_r1 ///
wealthindex_r1 age_r1 female_r1 ///
gap if match2==3 [pweight=attweight2]

*Figure 3.3, top left
preserve 
margins, at(uhururep_ch=(-3(1)3)) atmeans saving(margins, replace)

use margins, clear
replace _ci_lb = 0 if _ci_lb < 0
replace _ci_ub = 1 if _ci_ub > 1

scatter _margin _at1 || rspike _ci_lb _ci_ub _at1 || line _margin _at1, ///
xtitle("Change in Beliefs about Kenyatta") ytitle("Pr(Switch to Kenyatta)") title("") ///
legend(off) xscale(range(-3 3)) xlabel(-3(1)3) ///
scheme(s1mono) ylabel(0(.25)1, angle(0)) yscale(range(0 1)) yscale(noextend) plotregion(lcolor(white)) aspectratio(.5)  saving(A, replace)
restore

*Table 3.9, model 2
set more off
logit newraila uhururep_ch railarep_ch ///
uhururep_r1 railarep_r1 ///
likeu_ch liker_ch ///
pardiff spousediff networknco ncoshare ///
polint ed_r1 ///
radionews_r1 newspaper_r1 tvnews_r1 vernacular ///
wealthindex_r1 age_r1 female_r1 ///
gap if match2==3 [pweight=attweight2]

*Figure 3.3, top right
margins, at(railarep_ch=(-3(1)3)) atmeans saving(margins2, replace)

use margins2, clear
replace _ci_lb = 0 if _ci_lb < 0
replace _ci_ub = 1 if _ci_ub > 1

scatter _margin _at2 || rspike _ci_lb _ci_ub _at2 || line _margin _at2, ///
xtitle("Change in Beliefs about Odinga") ytitle("Pr(Switch to Odinga)") title("") ///
legend(off) xscale(range(-3 3)) xlabel(-3(1)3) ///
scheme(s1mono) ylabel(0(.25)1, angle(0)) yscale(range(0 1)) yscale(noextend) plotregion(lcolor(white)) aspectratio(.5)  saving(B, replace)

*Figure 3.3, top panel
graph combine A.gph B.gph, ycommon scheme(s1mono) xsize(10) iscale(*1.2) imargin(medium) title("2013 Election", size(medlarge)) name(comb2013, replace)


***2017 Election
use "panel2017.dta", clear

*Table 3.9, model 3
set more off
logit newuhuru uhururep_ch railarep_ch  ///
uhuru_rep_r1 odinga_rep_r1 ///
pardiff spousediff ncoshare  ///
polint ed ///
radio_r1 newspaper_r1 tv_r1 ///
house_income_r1 age_r2 female_r1 ///
[pweight=attweight2]

*Figure 3.3, bottom left
preserve 
margins, at(uhururep_ch=(-3(1)3)) atmeans saving(margins, replace)

use margins, clear
replace _ci_lb = 0 if _ci_lb < 0
replace _ci_ub = 1 if _ci_ub > 1

scatter _margin _at1 || rspike _ci_lb _ci_ub _at1 || line _margin _at1, ///
xtitle("Change in Beliefs about Kenyatta") ytitle("Pr(Switch to Kenyatta)") title("") ///
legend(off) xscale(range(-3 3)) xlabel(-3(1)3) ///
scheme(s1mono) ylabel(0(.25)1, angle(0)) yscale(range(0 1)) yscale(noextend) plotregion(lcolor(white)) aspectratio(.5)  saving(A, replace)

restore

*Table 3.9, model 4
set more off
logit newraila uhururep_ch railarep_ch  ///
uhuru_rep_r1 odinga_rep_r1 ///
pardiff spousediff ncoshare  ///
polint ed ///
radio_r1 newspaper_r1 tv_r1 ///
house_income_r1 age_r2 female_r1 ///
[pweight=attweight2]

*Figure 3.3, bottom left
margins, at(railarep_ch=(-3(1)3)) atmeans saving(margins2, replace)

use margins2, clear
replace _ci_lb = 0 if _ci_lb < 0
replace _ci_ub = 1 if _ci_ub > 1

scatter _margin _at2 || rspike _ci_lb _ci_ub _at2 || line _margin _at2, ///
xtitle("Change in Beliefs about Odinga") ytitle("Pr(Switch to Odinga)") title("") ///
legend(off) xscale(range(-3 3)) xlabel(-3(1)3) ///
scheme(s1mono) ylabel(0(.25)1, angle(0)) yscale(range(0 1)) yscale(noextend) plotregion(lcolor(white)) aspectratio(.5)  saving(B, replace)

*Figure 3.3, bottom panel
graph combine A.gph B.gph, ycommon scheme(s1mono) xsize(10)  iscale(*1.2) imargin(medium) title("2017 Election", size(medlarge)) name(comb2017, replace)

*Figure 3.3
graph combine combo2013 combo2017, cols(1) scheme(s1mono) xsize(10) ysize(7)









